package com.kaede.hayate.modules.api.controller;

import com.kaede.hayate.common.base.BaseController;
import com.kaede.hayate.config.ApplicationProperties;
import com.kaede.hayate.modules.api.dto.LoginRequest;
import com.kaede.hayate.security.entity.RestUserDetails;
import com.kaede.hayate.security.jwt.JwtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Api(tags = "登录模块")
@RestController
@RequestMapping("${application.api-prefix}")
public class LoginController extends BaseController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ApplicationProperties applicationProperties;

    @Autowired
    private JwtService jwtService;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @ApiOperation("用户登录")
    @PostMapping("login")
    public ResponseEntity login(@RequestBody LoginRequest loginRequest){
        logger.debug(passwordEncoder.encode(loginRequest.getPassword()));
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword());
        Authentication authentication = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
        RestUserDetails restUserDetails = (RestUserDetails) authentication.getPrincipal();
        String signedTokenSecret = jwtService.getSignedTokenSecret(restUserDetails.getTokenSecret());

        BoundValueOperations<String, String> valueOperations = stringRedisTemplate.boundValueOps(applicationProperties.getTokenSecretPrefix() + restUserDetails.getUsername());
        long tokenValidityInSeconds = applicationProperties.getTokenValidityInSeconds();
        if(loginRequest.getRemember()){
            tokenValidityInSeconds = applicationProperties.getTokenValidityInSecondsForRememberMe();
        }
        valueOperations.set(signedTokenSecret, tokenValidityInSeconds, TimeUnit.SECONDS);
        String token = jwtService.createToken(restUserDetails.getUserId(), restUserDetails.getUsername(), loginRequest.getRemember(), signedTokenSecret);
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        return ResponseEntity.ok(map);
    }

}
